package com.ruoyi.web.controller.common;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.phone.RegexUtility;
import com.ruoyi.framework.web.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @version :
 * @program: shipCargoEasyLink
 * @description: 短信接口
 * @author: 周爱鸿
 * @create: 2020-08-04 15:54
 **/
@RestController
@RequestMapping("/message")
@Api(description = "短信接口")
@Slf4j
public class MessageController {


	@Autowired
	private MessageService messageService ;
	/**
	 * 用户登录短信验证码
	 * @param phone 手机号
	 * @param userType 用户类型（00系统用户  11 app用户）
	 * @return
	 */
	@GetMapping("/getLoginVerificationCode")
	@ApiOperation("用户登录短信验证码")
	public AjaxResult getLoginVerificationCode(String phone , String userType){

		if(null == userType || "".equals(userType) || null == phone || "".equals(phone)){
			return AjaxResult.error("必要参数缺失");
		}
		if(!RegexUtility.checkMobilePhone(phone)){
			return AjaxResult.error("手机号格式错误");
		}
		String code = messageService.getLoginVerificationCode(phone, userType);
		return "OK".equals(code) ? AjaxResult.success("短信发送成功"): AjaxResult.error("短信发送失败,请重新发送");
	}

	/**
	 * app用户微信绑定手机号短信
	 * @param phone 手机号
	 * @return
	 */
	@GetMapping("/getWxBingPhoneVerificationCode")
	@ApiOperation("app用户微信绑定手机号短信")
	public AjaxResult getWxBingPhoneVerificationCode(String phone ){
		if(null == phone || "".equals(phone)){
			return AjaxResult.error("手机号不能为空");
		}
		if(!RegexUtility.checkMobilePhone(phone)){
			return AjaxResult.error("手机号格式错误");
		}
		String code = messageService.getWxBingPhoneVerificationCode(phone);
		return "OK".equals(code) ? AjaxResult.success("短信发送成功"): AjaxResult.error("短信发送失败,请重新发送");
	}

	/**
	 * app用户注册手机号短信
	 * @param phone 手机号
	 * @return
	 */
	@GetMapping("/getLogonVerificationCode")
	@ApiOperation("app用户注册手机号短信")
	public AjaxResult getLogonVerificationCode(String phone ){
		if(null == phone || "".equals(phone)){
			return AjaxResult.error("手机号不能为空");
		}
		if(!RegexUtility.checkMobilePhone(phone)){
			return AjaxResult.error("手机号格式错误");
		}
		String code = messageService.getLogonVerificationCode(phone);
		return "OK".equals(code) ? AjaxResult.success("短信发送成功"): AjaxResult.error("短信发送失败,请重新发送");
	}

	/**
	 * 忘记手机号验证码
	 * @param phone 手机号
	 * @return
	 */
	@GetMapping("/getForgetpasswordMessage")
	@ApiOperation("忘记手机号验证码")
	public AjaxResult getForgetpasswordMessage(String phone ){

		if(null == phone || "".equals(phone)){
			return AjaxResult.error("手机号不能为空");
		}
		if(!RegexUtility.checkMobilePhone(phone)){
			return AjaxResult.error("手机号格式错误");
		}

		String code = messageService.getForgetpasswordMessage(phone);
		return "OK".equals(code) ? AjaxResult.success("短信发送成功"): AjaxResult.error("短信发送失败,请重新发送");
	}
	/**
	 * 更换手机号验证码
	 * @param phone 手机号
	 * @return
	 */
	@GetMapping("/getChangePhoneMessage")
	@ApiOperation("更换手机号验证码")
	public AjaxResult getChangePhoneMessage(String phone ){

		if(null == phone || "".equals(phone)){
			return AjaxResult.error("手机号不能为空");
		}
		if(!RegexUtility.checkMobilePhone(phone)){
			return AjaxResult.error("手机号格式错误");
		}

		String code = messageService.getChangePhoneMessage(phone);
		return "OK".equals(code) ? AjaxResult.success("短信发送成功"): AjaxResult.error("短信发送失败,请重新发送");
	}
}
